package com.cristina.attention.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cristina.attention.entity.User;
import com.cristina.attention.exception.BadRequestException;
import com.cristina.attention.mapper.UserMapper;
import com.cristina.attention.utils.RedisUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @Author superman
 * @Date 2021/11/19 9:29
 **/
@Service
@Slf4j
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final RedisUtils redisUtils;
    private final PasswordEncoder passwordEncoder;

    @Override
    public boolean submit(User user) {
        int count = baseMapper.selectCount(Wrappers.lambdaQuery(User.class).eq(User::getUsername, user.getUsername()));
        if (count > 1) {
            throw new BadRequestException("当前用户已存在!");
        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return save(user);
    }

    @Override
    public boolean deleteLogic(@NotEmpty List<Long> ids) {
        return super.removeByIds(ids);
    }
}
